home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / MacShell / Init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-04  |  7.6 KB  |  250 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:         MacShell
  5. ** File:            init.c
  6. ** Some code from:  Traffic Light 2.0 (2.0 version by Keith Rollin)
  7. ** Modified by:     Eric Soldan
  8. **
  9. ** Copyright © 1989-1991 Apple Computer, Inc.
  10. ** All rights reserved.
  11. */
  12.  
  13.  
  14.  
  15. /*****************************************************************************/
  16.  
  17.  
  18.  
  19. #include "MacShell.h"            /* Get the MacShell includes/typedefs, etc.    */
  20. #include "MacShellCommon.h"        /* Get the stuff in common with rez.        */
  21. #include "MacShell.protos"        /* Get the prototypes for MacShell.            */
  22.  
  23. #ifndef __ERRORS__
  24. #include <Errors.h>
  25. #endif
  26.  
  27. #ifndef __GESTALTEQU__
  28. #include <GestaltEqu.h>
  29. #endif
  30.  
  31. #ifdef THINK_C
  32. #include "Utilities.h"
  33. #else
  34. #ifndef __UTILITIES__
  35. #include <Utilities.h>
  36. #endif
  37. #endif
  38.  
  39.  
  40.  
  41. /*****************************************************************************/
  42.  
  43.  
  44.  
  45. /* Set up the whole world, including global variables, Toolbox managers, and
  46. ** menus.  We also create our one application window at this time.  Since
  47. ** window storage is non-relocateable, how and when to allocate space for
  48. ** windows is very important so that heap fragmentation does not occur.
  49. */
  50.  
  51. /* The code that used to be part of ForceEnvirons has been moved into this
  52. ** module.  If an error is detected, instead of merely doing an ExitToShell,
  53. ** which leaves the user without much to go on, we call DeathAlert, which puts
  54. ** up a simple alert that just says an error occurred and then calls
  55. ** ExitToShell.  Since there is no other cleanup needed at this point if an
  56. ** error is detected, this form of error-handling is acceptable.  If more
  57. ** sophisticated error recovery is needed, an exception mechanism, such as is
  58. ** provided by Signals, can be used.
  59. */
  60.  
  61.  
  62.  
  63. /*****************************************************************************/
  64.  
  65.  
  66.  
  67. /* NOTE:  The “g” prefix is used to emphasize that a variable is global. */
  68.  
  69. Boolean    gQuitApplication;        /* Set to 0 by Initialize. */
  70.                                 /* Checked by EventLoop. */
  71.  
  72. extern Boolean        gHasAppleEvents;
  73. extern RgnHandle    gCursorRgn;
  74. extern short        gPrintPage;
  75.  
  76.  
  77. /*****************************************************************************/
  78. /*****************************************************************************/
  79.  
  80.  
  81.  
  82. #pragma segment Initialize
  83. void    Initialize(void)
  84. {
  85.     long        total, contig;
  86.  
  87.     StandardInitialization(1);            /* 1 MoreMasters. */
  88.         
  89.     /* Make sure that the machine has at least 128K ROMs.
  90.     ** If it doesn’t, exit.
  91.     */
  92.     
  93.     if ((gMachineType < gestaltMac512KE) || (!gHasWaitNextEvent))
  94.         DeathAlert(rBadNewsStrings, sWimpyMachine);
  95.     
  96.     /* We used to make a check for memory at this point by examining
  97.     ** ApplLimit, ApplicZone, and StackSpace and comparing that to the minimum
  98.     ** size we told MultiFinder we needed.  This did not work well because it
  99.     ** assumed too much about the relationship between what we asked
  100.     ** MultiFinder for and what we would actually get back, as well as how to
  101.     ** measure it.  Instead, we will use an alternate method comprised of
  102.     ** two steps.
  103.     */
  104.      
  105.     /* It is better to first check the size of the application heap against a
  106.     ** value that you have determined is the smallest heap the application can
  107.     ** reasonably work in.  This number should be derived by examining the
  108.     ** size of the heap that is actually provided by MultiFinder when the
  109.     ** minimum size requested is used.  The derivation of the minimum size
  110.     ** requested from MultiFinder is described in MacShell.h.  The check should
  111.     ** be made because the preferred size can end up being set smaller than
  112.     ** the minimum size by the user.  This extra check acts to ensure that
  113.     ** your application is starting from a solid memory foundation.
  114.     */
  115.      
  116.     if ((long) GetApplLimit() - (long) ApplicZone() < kMinHeap)
  117.         DeathAlert(rBadNewsStrings, sHeapTooSmall);
  118.     
  119.     /* Next, make sure that enough memory is free for your application to run.
  120.     ** It is possible for a situation to arise where the heap may have been of
  121.     ** required size, but a large scrap was loaded which left too little
  122.     ** memory.  To check for this, call PurgeSpace and compare the result with
  123.     ** a value that you have determined is the minimum amount of free memory
  124.     ** your application needs at initialization.  This number can be derived
  125.     ** several different ways.  One way that is fairly straightforward is to
  126.     ** run the application in the minimum size configuration as described
  127.     ** previously.  Call PurgeSpace at initialization and examine the value
  128.     ** returned.  However, you should make sure that this result is not being
  129.     ** modified by the scrap’s presence.  You can do that by calling ZeroScrap
  130.     ** before calling PurgeSpace.  Make sure to remove that call before
  131.     ** shipping, though.
  132.     */
  133.     
  134.     /* ZeroScrap(); */
  135.  
  136.     PurgeSpace(&total, &contig);
  137.     if (total < kMinSpace)
  138.         DeathAlert(rBadNewsStrings, sNoFreeRoomInHeap);
  139.  
  140.     /* The extra benefit to waiting until after the Toolbox Managers have been
  141.     ** initialized to check memory is that we can now give the user an alert
  142.     ** to tell him/her what happened.  Although it is possible that the memory
  143.     ** situation could be worsened by displaying an alert, MultiFinder would
  144.     ** gracefully exit the application with an informative alert if memory
  145.     ** became critical.  Here we are acting more in a preventative manner to
  146.     ** avoid future disaster from low-memory problems.
  147.     */
  148.  
  149.     StandardMenuSetup(rMenuBar, mApple);
  150.     AdjustMenus();
  151.  
  152.     ATInit();
  153.  
  154.     InitRequiredAppleEvents();
  155.     InitConnectAppleEvents();
  156.     InitCustomAppleEvents();
  157.  
  158.     gCursorRgn  = NewRgn();        /* The current cursor region. */
  159.     DoSetCursor(&qd.arrow);
  160.  
  161.     qd.randSeed = TickCount();
  162.     gQuitApplication = false;    /* We are only starting.  Don't quit now! */
  163. }
  164.  
  165.  
  166.  
  167. /*****************************************************************************/
  168.  
  169.  
  170.  
  171. /* This function handles the documents selected in the finder, either for
  172. ** loading or for printing.  This is only if we don't have AppleEvents.
  173. ** If we have AppleEvents, then this will all be done automatically via
  174. ** those wonderful AppleEvent thingies.
  175. */
  176.  
  177. #pragma segment Initialize
  178. void    StartDocuments(void)
  179. {
  180.     OSErr        err;
  181.     short        i;
  182.     short        whatToDo;
  183.     short        numberOfFiles;
  184.     AppFile        theAppFile;
  185.     FSSpec        fileSpec;
  186.     long        ignoredProcID;
  187.     FileRecHndl    frHndl;
  188.     WindowPtr    docWindow;
  189.     
  190.     err = noErr;
  191.  
  192.     if (!gHasAppleEvents) {
  193.  
  194.         CountAppFiles(&whatToDo, &numberOfFiles);
  195.  
  196.         if (numberOfFiles > 0) {
  197.  
  198.             for (i = 1; (i <= numberOfFiles) && (!err); ++i) {
  199.                 GetAppFiles(i, &theAppFile);
  200.                 ClrAppFiles(i);
  201.                 err = GetWDInfo(theAppFile.vRefNum,
  202.                                 &fileSpec.vRefNum,
  203.                                 &fileSpec.parID,
  204.                                 &ignoredProcID);
  205.  
  206.                 if (err) Alert(rErrorAlert, (ModalFilterProcPtr)alertFilter);
  207.  
  208.                 else {
  209.                     BlockMove((Ptr)&theAppFile.fName,
  210.                               (Ptr) &fileSpec.name,
  211.                               theAppFile.fName[0] + 1
  212.                     );
  213.  
  214.                     err = AppOpenDocument(&frHndl, &fileSpec, fsRdWrPerm);
  215.                     if (!err) {
  216.                         gPrintPage = whatToDo;
  217.                             /* Open the window off-screen if we are printing. */
  218.                         if (err = DoNewWindow(frHndl, &docWindow, (WindowPtr)-1, kwAppWindow))
  219.                             AppDisposeDocument(frHndl);
  220.                         else {
  221.                             if (gPrintPage) {
  222.                                 err = AppPrintDocument(frHndl, (i == 1), (i == 1));
  223.                                 AppDisposeDocument(frHndl);
  224.                                 DisposeAnyWindow(docWindow);
  225.                             }
  226.                         }
  227.                         gPrintPage = 0;
  228.                     }
  229.                     if ((err) && (err != userCanceledErr)) {
  230.                         Alert(rErrorAlert, (ModalFilterProcPtr)alertFilter);
  231.                         break;
  232.                     }
  233.                 }
  234.             }
  235.             if (whatToDo == appPrint) gQuitApplication = true;
  236.         }
  237.         else {
  238.             err = AppNewDocument(&frHndl, docFileType);
  239.             if (!err)
  240.                 if (err = DoNewWindow(frHndl, nil, (WindowPtr)-1, kwAppWindow))
  241.                     AppDisposeDocument(frHndl);
  242.         }
  243.     }
  244.  
  245.     AppPrintDocument(nil, false, false);    /* Clean up after printing, if we did any. */
  246. }
  247.  
  248.  
  249.  
  250.